package com.tw.management.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.Email;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tw.management.entity.common.AbstractEntity;

/**
 * 用户
 * 
 * @author TangW
 * @version 1.0
 *
 */
@Entity(name = "sys_users")
public class User extends AbstractEntity implements Serializable {

	private static final long serialVersionUID = 1525673412130131146L;

	private String userType;

	@NotNull(message = "{user.email.null.error}")
	@Email(message = "{user.email.pattern.error}")
	private String email;

	@JsonIgnore
	@NotNull(message = "{user.password.null.error}")
	// @Length(min = 6, max = 20, message = "{user.password.length.error}")
	private String password;

	private String salt; // 随机数

	private Boolean locked = Boolean.FALSE;

	private String realname;

	private String gender = "0";

	@Past
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date birthDate;

	private String mobile;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createDate;

	// ----------------------------------------------//

	/**
	 * 加密密码时使用 email + salt 作为干扰数据
	 * 
	 * @return email + salt
	 */
	public String getCredentialsSalt() {
		return email + salt;
	}

	// ----------------------------------------------//

	public User() {
		super();
	}

	public User(String email, String password) {
		super();
		this.email = email;
		this.password = password;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public Boolean getLocked() {
		return locked;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
